package com.cskaoyan.wordmemorize.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cskaoyan.wordmemorize.common.aspect.operate.OperateLog;
import com.cskaoyan.wordmemorize.common.aspect.operate.OperateLogConstant;
import com.cskaoyan.wordmemorize.converter.LevelConverter;
import com.cskaoyan.wordmemorize.dao.entity.LevelDO;
import com.cskaoyan.wordmemorize.dao.mapper.LevelMapper;
import com.cskaoyan.wordmemorize.dto.admin.DeleteLevelOperateDTO;
import com.cskaoyan.wordmemorize.dto.admin.LevelDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.request.BatchDeleteCommand;
import com.cskaoyan.wordmemorize.request.LevelCommand;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.service.LevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LevelServiceImpl implements LevelService {

    @Autowired
    LevelMapper levelMapper;
    @Autowired
    LevelConverter levelConverter;
    @Override
    @OperateLog(dateType = "等级", operateType = OperateLogConstant.OPERATE_ADD)
    public void addLevel(LevelCommand command) {
        LevelDO levelDO = levelConverter.levelCommandToLevelDO(command);
        levelMapper.insert(levelDO);
    }

    @Override
   @OperateLog(dateType = "等级", operateType = OperateLogConstant.OPERATE_UPDATE)
    public void updateLevel(LevelCommand command) {
        LevelDO levelDO = levelConverter.levelCommandToLevelDO(command);
        levelMapper.updateById(levelDO);
    }

    @Override
    public PageDTO<LevelDTO> getLevelList(PageRequest request) {
        QueryWrapper<LevelDO> wrapper = new QueryWrapper<>();
        Page<LevelDO> page = new Page<>(request.getPageNum(), request.getCount());
        if (request.getKeyword() != null){
            wrapper.like("level_name", request.getKeyword());
            Page<LevelDO> levelDOPage = levelMapper.selectPage(page, wrapper);
            return levelConverter.levelDOToPageDTO(levelDOPage.getRecords(), (int) levelDOPage.getTotal());
        }else {
            wrapper.like("level_name", request.getKeyword());
            Page<LevelDO> levelDOPage = levelMapper.selectPage(page, null);
            return levelConverter.levelDOToPageDTO(levelDOPage.getRecords(), (int) levelDOPage.getTotal());
        }
    }

    @Override
    @OperateLog(dateType = "等级", operateType = OperateLogConstant.OPERATE_SINGLE_DELETE)
    public DeleteLevelOperateDTO deleteLevel(Long id) {
        LevelDO levelDO = levelMapper.selectById(id);
        //日志对象
        DeleteLevelOperateDTO deleteLevelOperateDTO = levelConverter.levelDOToDeleteLevelOperateDTO(levelDO);
        int i = levelMapper.deleteById(id);

        return deleteLevelOperateDTO;
    }

    @Override
    @OperateLog(dateType = "等级", operateType = OperateLogConstant.OPERATE_BATCH_DELETE)
    public List<DeleteLevelOperateDTO> deleteLevels(BatchDeleteCommand command) {
        List<LevelDO> levelDOS = levelMapper.selectByIds(command.getIds());
        //日志对象
        List<DeleteLevelOperateDTO> deleteLevelOperateDTOS = levelConverter.levelDOsToDeleteLevelOperateDTOs(levelDOS);

        int i = levelMapper.deleteByIds(command.getIds());

        return deleteLevelOperateDTOS;
    }
}
